﻿//将C#DateTime类型的返回数据截取成yyyy-MM-dd hh:mm:ss格式
$.extend({
    getDateFormat: function(data) {
        return $.formatDate(new Date(data), "yyyy-MM-dd HH:mm:ss");
    },
    getShortDateFormat: function(data) {
        return $.formatDate(new Date(data), "yyyy-MM-dd");
    },
});

//将C#TimeSpan类型的返回数据截取成xx:xx:xx格式
$.extend({
    getTimeSpanFormat: function(data) {
        return (data + "").substring(0, 8);
    },

    getTimeSpanFormatOfSeconds: function(data) {
        //超过24小时的予以忽略
        var second = data % 60;
        var temp = Math.floor(data / 60);
        var minute = temp % 60;
        temp = Math.floor(temp / 60);
        var hour = temp % 24;
        var result = $.padLeft(hour.toString(), 2, '0')
            + ":" + $.padLeft(minute.toString(), 2, '0')
            + ":" + $.padLeft(second.toString(), 2, '0');
        return result;
    },
});

$.extend({
    getWeekStartEnd: function() {
        var time = new Date(), msg = [];
        time.setDate(time.getDate() - time.getDay() + 1);
        msg.push($.getShortDateFormat(time));
        time.setDate(time.getDate() + 6);
        msg.push($.getShortDateFormat(time));
        return msg;
    },
    getMonthStartEnd: function() {
        var time = new Date(), msg = [];
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        var firstDay = month + "/" + "01/" + year;
        msg.push($.getShortDateFormat(firstDay));

        time = new Date(year, month, 0);
        var lastDay = month + "/" + time.getDate() + "/" + year;
        msg.push($.getShortDateFormat(lastDay));
        return msg;
    },
    getYearStartEnd: function() {
        var time = new Date(), msg = [];
        var year = time.getFullYear();
        var firstDay = "01/01/" + year;
        msg.push($.getShortDateFormat(firstDay));
        var lastDay = "12/01/" + year;
        msg.push($.getShortDateFormat(lastDay));
        return msg;
    },
});

$.extend({
    enableRead: function(dom) {
        dom.attr({ readonly: "true" });
    },
    disableRead: function(dom) {
        dom.removeAttr("readonly");
    },
});

$.extend({
    padLeft: function(data, totalWidth, paddingChar) {
        if (paddingChar != null) {
            return $.padHelper(data, totalWidth, paddingChar, false);
        } else {
            return $.padHelper(data, totalWidth, ' ', false);
        }
    },
    padRight: function(data, totalWidth, paddingChar) {
        if (paddingChar != null) {
            return $.padHelper(data, totalWidth, paddingChar, true);
        } else {
            return $.padHelper(data, totalWidth, ' ', true);
        }
    },
    padHelper: function(data, totalWidth, paddingChar, isRightPadded) {
        if (data.length < totalWidth) {
            var paddingString = new String();
            for (var i = 1; i <= (totalWidth - data.length); i++) {
                paddingString += paddingChar;
            }
            if (isRightPadded) {
                return (data + paddingString);
            } else {
                return (paddingString + data);
            }
        } else {
            return data;
        }
    }
});